import axios from 'axios'
import {ElMessage} from "element-plus";

const URL = "http://123.56.19.79:8080"

const http = axios.create({
    baseURL:URL,
    timeout:5000
})

http.interceptors.request.use(
    config =>{
        config.headers['token'] = ""
        return config
    },
    error =>{
        return Promise.reject(error)
    }
)

http.interceptors.response.use(
    response =>{
        const res = response.data
        console.log("响应数据",res)
        if (res.code !== 0){
            if(res.code === 401){
                ElMessage({
                    message:"未登录或登陆已过期，请重新登陆"
                })
            }
            ElMessage({
                message: res.message || 'Error',
                type: 'error',
                duration: 5 * 1000
            })
            return Promise.reject(new Error(res.message || 'Error'))
        }else {
            return res
        }
    },
    error => {
        console.log('err' + error) // for debug
        ElMessage({
            message: error.message,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
)

export default http
